package com.cetool.autocms.util;

import com.cetool.autocms.exception.ServiceException;
import com.cetool.autocms.pojo.domain.login.UserInfoDO;
import com.cetool.autocms.service.enumeration.LoginExceptionEnum;

import java.util.Date;
import java.util.Objects;

/**
 * 用户信息工具类
 *
 * @author 戴越
 * @date 2021/11/16
 */
public class UserInfoContextUtil {
    /**
     * 用户信息线程变量
     */
    public static final ThreadLocal<UserInfoDO> CONTEXT = new ThreadLocal<>();

    /**
     * 构造器私有，防止外部实例化
     */
    private UserInfoContextUtil() {

    }

    public static UserInfoDO getUserData() {
        UserInfoDO sysUserInfoDO = CONTEXT.get();
        if (Objects.isNull(sysUserInfoDO)) {
            throw new ServiceException(LoginExceptionEnum.LOGIN_FAILED_USER_NOT_EXIST);
        }
        return sysUserInfoDO;
    }

    public static void setUser(UserInfoDO user) {
        if (Objects.isNull(user)) {
            throw new ServiceException(LoginExceptionEnum.LOGIN_FAILED_USER_NOT_EXIST);
        }
        CONTEXT.set(user);
    }

    public static String getUserId() {
        return getUserData().getUserId();
    }

    public static String getUserName() {
        return getUserData().getUserName();
    }

    public static String getUserPassWord() {
        return getUserData().getUserPassWord();
    }

    public static Integer getRoleId() {
        return getUserData().getRoleId();
    }

    public static String getCompanyName() {
        return getUserData().getCompanyName();
    }

    public static Date getValidDate() {
        return getUserData().getValidDate();
    }

    public static Date getCreateDate() {
        return getUserData().getCreateDate();
    }

    public static Integer getAccountState() {
        return getUserData().getAccountState();
    }

    public static String getClientUserId() {
        return getUserData().getClientUserId();
    }

    public static void clear() {
        CONTEXT.remove();
    }
}
